package student.action;

import java.util.ArrayList;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.DB;
import com.util.MyLog;

import edu.student.models.Enrollment;
import edu.student.models.Lookup;
import edu.student.models.Semester;
import edu.student.models.Subject;
import edu.student.models.User;

public class AdminUserGrades extends ActionSupport{
	private String rollNo;
	private String mySemester;
	private String myCourse;
	private String grade;
	private String gradePt;
	ArrayList<Enrollment> selection;
	ArrayList<String> userList;
	private String result;
	private ArrayList<String> semesterList ;
	private ArrayList<String> courseList  ;
	private String commandButton = "";
	private String msg="";
	
	
	
	public String getRollNo() {
		return rollNo;
	}



	public void setRollNo(String rollNo) {
		this.rollNo = rollNo;
	}



	public String getMySemester() {
		return mySemester;
	}



	public void setMySemester(String mySemester) {
		this.mySemester = mySemester;
	}



	public String getMyCourse() {
		return myCourse;
	}



	public void setMyCourse(String myCourse) {
		this.myCourse = myCourse;
	}



	public String getGrade() {
		return grade;
	}



	public void setGrade(String grade) {
		this.grade = grade;
	}

	

	public String getGradePt() {
		return gradePt;
	}



	public void setGradePt(String gradePt) {
		this.gradePt = gradePt;
	}



	public ArrayList<Enrollment> getSelection() {
		return selection;
	}



	public void setSelection(ArrayList<Enrollment> selection) {
		this.selection = selection;
	}



	public ArrayList<String> getUserList() {
		return userList;
	}



	public void setUserList(ArrayList<String> userList) {
		this.userList = userList;
	}



	public String getResult() {
		return result;
	}



	public void setResult(String result) {
		this.result = result;
	}



	public ArrayList<String> getSemesterList() {
		return semesterList;
	}



	public void setSemesterList(ArrayList<String> semesterList) {
		this.semesterList = semesterList;
	}



	public ArrayList<String> getCourseList() {
		return courseList;
	}



	public void setCourseList(ArrayList<String> courseList) {
		this.courseList = courseList;
	}



	public String getCommandButton() {
		return commandButton;
	}



	public void setCommandButton(String commandButton) {
		this.commandButton = commandButton;
	}



	public String getMsg() {
		return msg;
	}



	public void setMsg(String msg) {
		this.msg = msg;
	}



	public String execute() throws Exception {
		MyLog.log("Inside AdminUserGrades : Method : execute");
		System.out.println("I am in execute of grades!!!!");
		courseList=Enrollment.getSubjectList();
		semesterList=Enrollment.getSemesterList();
		userList=User.userListDetails("");
		//gradePt=0;
		/*ArrayList<Enrollment> enroll = Enrollment.getRecords();
		//print the values
		System.out.println("enroll list size is "+enroll.size());
		for(int i=0;i<enroll.size();i++){
			Enrollment enr = new Enrollment();
			enr=enroll.get(i);
			System.out.println("printing enrollment details"+enr.getRollNo()+" "+enr.getSemesterName()+" "+enr.getSubjectName());
			//if the course is already not in the list add it
			int a=0,b=0;
			for(int j=0;j<courseList.size();j++){
				if(courseList.get(j) != enr.getSubjectName()) 
				a=1;
			}
			if(a==1) courseList.add(enr.getSubjectName());
			System.out.println("m here in java home page"+enr.getSubjectName());
			for(int j=0;j<semesterList.size();j++){
				if(semesterList.get(j) != enr.getSemesterName()) b=1;
			}
			if(b==1) semesterList.add(enr.getSemesterName());
			System.out.println("m in java home afetr semester list"+enr.getSemesterName());
		}*/
		//if rollNo is not blank get the list of semesterid
		if(!rollNo.isEmpty() ) semesterList = Enrollment.getSemesterListForUser(rollNo);
		
		//if semester is not empty gets the subject list for which student is enrolled.
		if(!mySemester.isEmpty()){
			int semesterId = Semester.getSemesterId(mySemester); 
			courseList = Enrollment.getSubjectListForSemester(rollNo,semesterId);
		}
		
		//if view records is clicked
		if (this.commandButton.startsWith("View")){
			/*fetches the value from enrollment table and displays*/
			System.out.println("in view records page");
			String selectionModifier = "";
			if(!mySemester.isEmpty()){
				int semesterId = Semester.getSemesterId(mySemester);
				selectionModifier = selectionModifier + " and semester_id = "+semesterId+" ";
			}
			if(!this.myCourse.isEmpty()){
				int subjectId = Subject.getSubjectId(myCourse);
				selectionModifier = selectionModifier + " and subject_id = "+subjectId+"";
			}
			selection=Enrollment.getRecords(selectionModifier);
			System.out.println("In view Records in admin user grades page");
			for(int i=0;i<selection.size();i++){
				Enrollment sel = selection.get(i);
				System.out.println(sel.getRollNo());
			}
			return "viewRecords";
		}
		//if Back button is clicked
		else if (this.commandButton.startsWith("Back")){
			return "success";
		}
		//if values are selected for each field
		else if(!this.rollNo.isEmpty() && !this.mySemester.isEmpty() && !this.myCourse.isEmpty()) {
			System.out.println("printing roll no value"+rollNo);
        	
			//get semester id for the semester name
	      	int semesterId = Semester.getSemesterId(mySemester);
			
	      	/*get the subject id from subject table for the chosen subject*/
			int subjectId = Subject.getSubjectId(myCourse);
			
			//if save button is clicked
			if (this.commandButton.startsWith("Save")) {
				/**Check if the data is already there for the subject
				 * if its there, show a msg indicating the value is already there**/
				int res1 = Enrollment.checkDetails(rollNo, semesterId, subjectId);
				if(res1 != 0){
					msg="Grade already added for the selected subject.It can be modified on View Records Page";
					return "success";
				}
				/*Insert the values grade and result in enrollment table.update query*/
				
				int res = Enrollment.updateResultInEnroll(rollNo, semesterId, subjectId, grade, result,gradePt);
				if(res==1) {
					msg="Result updated for student";
					return "success";
				}
				else { msg="Student is not enrolled for the course "+myCourse+" in semester "+mySemester;
						return "error";
				}
			}
				
				
		
	      }
		return "initial";
	 	}
}
